package com.yonghong.groupreport.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.yonghong.groupreport.config.DynamicDataSource;
import com.yonghong.groupreport.domain.PaOrganize;
import com.yonghong.groupreport.domain.PaReport;
import com.yonghong.groupreport.service.IPaReportService;
import com.yonghong.groupreport.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yonghong.groupreport.utils.BaseController;

/**
 * 报表信息Controller
 * 
 * @author yh
 * @date 2023-12-29
 */
@RestController
@RequestMapping("/reports/report")
public class PaReportController extends  BaseController
{
    @Autowired
    private IPaReportService paReportService;
    @Autowired
    private DynamicDataSource dynamicDataSource;

//    /**
//     * 查询报表信息列表
//     */
//
//    @GetMapping("/list")
//    public AjaxResult list(PaReport paReport)
//    {
//
//        List<PaReport> list = paReportService.selectPaReportList(paReport);
//        return success(list);
//    }
//
//    /**
//     * 查询报表信息列表
//     */
//
//    @GetMapping("/listReport")
//    public AjaxResult listReport()
//    {
//        //startPage();
//        List<PaReport> list = paReportService.listReports();
//        return success(list);
//    }
//
//
//
//    /**
//     * 获取报表信息详细信息
//     */
//
//    @GetMapping(value = "/{paReportId}")
//    public AjaxResult getInfo(@PathVariable("paReportId") Long paReportId)
//    {
//        return success(paReportService.selectPaReportByPaReportId(paReportId));
//    }

    /**
     * 新增报表信息
     */


    @PostMapping
    public AjaxResult add(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException {
        if(ajaxResult.get("msg").toString()!=null)
        {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }
        int  rows=0;
        PaReport paReport= JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")),PaReport.class);

//        PaOrganize paOrganize= (PaOrganize) ajaxResult.get("paOrganize");

        if(ajaxResult.get("msg").equals("NC"))
        {
            rows=paReportService.insertPaReportOracle(paReport);
        }
        else {
            rows=paReportService.insertPaReport(paReport);
        }
        return toAjax(rows);

    }

    /**
     * 修改报表信息
     */


    @PutMapping
    public AjaxResult edit(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException {

        int  rows=0;
        if(ajaxResult.get("msg").toString()!=null)
        {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }
        PaReport paReport = paReport = JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")), PaReport.class);;
        if(ajaxResult.get("msg").equals("NC"))
        {
            rows=paReportService.updatePaReportOracle(paReport);
        }
        else {
            rows=paReportService.updatePaReport(paReport);
        }
        return toAjax(rows);
    }

    /**
     * 删除报表信息
     */


	@DeleteMapping
    public AjaxResult remove(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException, JSONException {
        if (ajaxResult.get("msg") != null) {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }

        JSONArray jsonArr = new JSONArray(ajaxResult.get("data").toString()); // 创建JSONArray对象并传入JSON字符串

        int length = jsonArr.length(); // 获取JSON数组长度
        Long[] orgIds = new Long[length]; // 定义存放结果的long类型数组

        for (int i = 0; i < length; i++) {
            orgIds[i] = jsonArr.getLong(i); // 遍历JSON数组，依次提取每个元素并赋值到long数组中
        }


        return toAjax(paReportService.deletePaReportByPaReportIds(orgIds));
    }
}
